package com.tinckay.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinckay.common.annotation.Log;
import com.tinckay.common.constant.ProjectConstant;
import com.tinckay.common.util.LocalDateTimeUtils;
import com.tinckay.common.util.ShiroUtils;
import com.tinckay.entity.po.Task;
import com.tinckay.entity.po.TranProcess;
import com.tinckay.entity.po.User;
import com.tinckay.service.TranProcessService;
import com.tinckay.service.VehicleService;
import com.tinckay.service.impl.TaskServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jean
 * @since 2018-10-24
 */
@Api(tags = "运输过程管理")
@RestController
@RequestMapping("/tranProcess")
public class TranProcessController extends ApiController {

    @Autowired
    private TranProcessService tranProcessService;
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private TaskServiceImpl taskService;

    @Log("删除运输过程")
    @ApiIgnore
    @DeleteMapping
    @ApiOperation("删除运输过程")
    //@RequiresPermissions("tranProcess:delete")
    public R delete(@ApiParam("运输过程id数组") @RequestParam Long[] ids) {
        if (ArrayUtil.isEmpty(ids)) {
            return failed("参数id不能为空.");
        }
        tranProcessService.removeByIds(CollectionUtil.toList(ids));
        return success(null);
    }

    @Log("新增运输过程")
    @PostMapping
    @ApiOperation(value = "新增运输过程", notes = " 1装料 2运输 3卸料 4卸料暂停 5返回,运输状态为0则自动产生一条运输任务并返回.")
    //@RequiresPermissions("tranProcess:save")
    public R save(@RequestBody @Validated(ProjectConstant.Save.class) TranProcess tranProcess) {
        if (0 == tranProcess.getProcessType()) {
            //TODO 仓体编号待确定
            Task task = new Task();
            User user = ShiroUtils.getCurrentUser();
            task.setUserId(user.getId());
            task.setDriverName(user.getName());
            task.setVehNo(vehicleService.findVehNo(user.getId()));
            task.setBarnNo(null);
            task.setTaskStatus(0);

            taskService.save(task);
            return success(task);
        }
        if (!LocalDateTimeUtils.checkStartTimeAndEndTime(tranProcess.getStartTime(), tranProcess.getEndTime())) {
            return failed("起始时间验证有误");
        }
        if (tranProcess.getProcessType() == null) {
            return failed("运输过程状态不能为空");
        }
        tranProcessService.saveTranProcess(tranProcess);
        return success(null);
    }

    @Log("更新运输过程")
    @PutMapping
    @ApiIgnore
    @ApiOperation("更新运输过程")
    //@RequiresPermissions("tranProcess:update")
    public R update(@RequestBody @Validated(ProjectConstant.Update.class) TranProcess tranProcess) {
        tranProcessService.updateById(tranProcess);
        return success(tranProcess);
    }

    @GetMapping("/list")
    @ApiOperation("分页查询运输过程列表")
    //@RequiresPermissions("tranProcess:page")
    public R<IPage> list(@RequestParam(defaultValue = "1") int current,
                         @RequestParam(defaultValue = "10") int size) {
        IPage<TranProcess> page = tranProcessService.page(new Page<>(current, size), null);
        return success(page);
    }

    @GetMapping("/info/{tranProcessId}")
    @ApiOperation("获取运输过程信息")
    //@RequiresPermissions("tranProcess:info")
    public R<TranProcess> info(@PathVariable("tranProcessId") long tranProcessId) {
        return success(tranProcessService.getById(tranProcessId));
    }

}

